﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Ninject;
using Ninject.Modules;

namespace CoupingDemo
{
    class Program
    {
        static void Main(string[] args)
        {

            //紧耦合版本
            var resetHelperV1 = new PasswordResetHelperV1();
            resetHelperV1.ResetPassword();
          
            //松耦合版本,没有使用IOC
            var resetHelperV2 = new PasswordResetHelperV2(new UnionEmailSender());
            resetHelperV2.ResetPassword();

            //使用IOC容器，开发现代程序
            var kernel = new StandardKernel(new CoupleModule());
            var helperV2 = kernel.Get<PasswordResetHelperV2>();
            helperV2.ResetPassword();
        }
    }

    class CoupleModule:NinjectModule
    {
        public override void Load()
        {
            Bind<IEmailSender>().To<UnionEmailSender>();
            Bind<PasswordResetHelperV2>().ToSelf().InSingletonScope();
        }
    }
    /// <summary>
    /// 紧耦合关系
    /// </summary>
    public class PasswordResetHelperV1
    {
        private UnionEmailSender _telcomEmailSender;

        public PasswordResetHelperV1()
        {
            //依赖于TelcomEmailSender,PasswordResetHelperV1与TelcomEmailSender产生紧耦合关系;
            _telcomEmailSender = new UnionEmailSender();
        }
        public void ResetPassword()
        {

            //把新的密码发送给用户邮箱
            _telcomEmailSender.Send();
        }

    }
    /// <summary>
    /// 松耦合关系
    /// </summary>
    public class PasswordResetHelperV2
    {
        private readonly IEmailSender _emailSender;

        /// <summary>
        /// PasswordResetHelperV2通过构造函数与IEmailSender建立一种耦合关系
        /// </summary>
        /// <param name="emailSender"></param>
        public PasswordResetHelperV2(IEmailSender emailSender)
        {
            _emailSender = emailSender;
            
        }

        public void ResetPassword()
        {
            _emailSender.Send();
        }
    }

    public class TelcomEmailSender:IEmailSender
    {
        public void Send()
        {
            //把新的密码发送给用户邮箱
        }
    }

    public interface IEmailSender
    {
        void Send();
    }

    public class UnionEmailSender : IEmailSender
    {
        public void Send()
        {
            //把新的密码发送给用户邮箱
            Console.WriteLine("密码重置成功");
        }
    }
    //如果你是人，则必须能吃饭
    //定义什么是人
    interface IPerson
    {
        void Eat();
    }

    interface ISwimmer
    {
        void Swim();
    }
    //Person实现人的定义
    class Person : IPerson,ISwimmer
    {
        public void Eat()
        {
            Console.WriteLine("吃饭");
        }

        public void Swim()
        {
            throw new NotImplementedException();
        }
    }
}
